<?php
	require('curriculum.doc.php');
	//教练列表
	$app->get(
		'/curriculum/coach/list',
		function() use($app, $db2, $curriculumtype){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to'])){
				// $sqlwhere['tb_coach.state'] = 1;
				// $sqlwhere['group']='tb_curriculum.coach_id';
				// if(isset($data['nickname'])){
				// 	$sqlwhere['like']=array('tb_coach.nickname'=>array('%',$data['nickname'],'%'));
				// }
				// if(isset($data['sex'])){
				// 	$sqlwhere['tb_coach.sex']=$data['sex'];
				// }
				// if(isset($data['type'])){
				// 	$sqlwhere['tb_curriculum.type']=$data['type'];
				// }
				// $echo['count'] = $db2->select(2, 'tb_curriculum',array('[><]tb_coach'=>array('coach_id'=>'tbid')), '*', $sqlwhere);
				// $sqlwhere['limit'] = array($data['from'], $data['to']);
				// $sqlwhere['order'] = 'tb_coach.level desc';
				// $coach=$db2->select(0,'tb_curriculum',array('[><]tb_coach'=>array('coach_id'=>'tbid')),'tb_coach.tbid,tb_coach.nickname,tb_coach.sex,tb_coach.title,tb_coach.level,('.$db2->select(4,'tb_curriculum','GROUP_CONCAT(DISTINCT type)[type]',array('#tb_curriculum.coach_id'=>'tb_coach.tbid')).')[type]',$sqlwhere);
				// foreach($coach as $v){
				// 	$tmp['coachid'] = $v['tbid'];
				// 	$tmp['head'] = file_exists('dofiles/coach/'.$v['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$v['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
				// 	$tmp['nickname'] = $v['nickname'];
				// 	$tmp['sex'] = $v['sex'];
				// 	$tmp['title'] = $v['title'];
				// 	$tmp['level'] = $v['level'];
				// 	$type=explode(',',$v['type']);
				// 	foreach($type as $t){
				// 		$types[]=$curriculumtype[$t];
				// 	}
				// 	$tmp['type'] = implode(',',$types);
				// 	$echo['data'][] = $tmp;
				// 	unset($tmp);
				$sqlwhere['state'] = 1;
				if(isset($data['nickname'])){
					$sqlwhere['like']=array('nickname'=>array('%',$data['nickname'],'%'));
				}
				if(isset($data['sex'])){
					$sqlwhere['sex']=$data['sex'];
				}
				$echo['count'] = $db2->select(2, 'tb_coach', '*', $sqlwhere);
				$sqlwhere['limit'] = array($data['from'], $data['to']);
				$sqlwhere['order'] = 'level desc';
				$coach=$db2->select(0,'tb_coach','*',$sqlwhere);
				foreach($coach as $v){
					$tmp['coachid'] = $v['tbid'];
					$tmp['head'] = file_exists('dofiles/coach/'.$v['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$v['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
					$tmp['nickname'] = $v['nickname'];
					$tmp['sex'] = $v['sex'];
					$tmp['title'] = empty($v['title'])?"暂无":$v['title'];
					$tmp['level'] = $v['level'];
					// $type=explode(',',$v['type']);
					// foreach($type as $t){
					// 	$types[]=$curriculumtype[$t];
					// }
					// $tmp['type'] = implode(',',$types);
					$tmp['type']="暂无";
					$echo['data'][] = $tmp;
					unset($tmp);
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//教练详情
	$app->get(
		'/curriculum/coach/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			$echo['curriculum'] = array();
			if($data && isset($data['coachid'])){
				$coach = $db2->select(1, 'tb_coach', '*', array('tbid' => $data['coachid']));
				$echo['head'] = file_exists('dofiles/coach/'.$coach['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/coach/'.$coach['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
				$echo['nickname'] = $coach['nickname'];
				$echo['sex'] = $coach['sex'];
				$echo['title'] = $coach['title'];
				$echo['intro'] = $coach['intro'];
				$echo['level'] = $coach['level'];
				$echo['sex'] = $coach['sex'];
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程列表
	$app->get(
		'/curriculum/list',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['from']) && isset($data['to'])){
				$sqlwhere=array('coach_id' => $data['coachid'],'order'=>'tbid desc');
				if($data['type']!=1){
					$sqlwhere['state']=1;
				}
				$echo['count'] = $db2->select(2, 'tb_curriculum', '*', $sqlwhere);
				$sqlwhere['limit'] = array($data['from'], $data['to']);
				$sqlwhere['order'] = "state asc,tbid desc";
				$curriculum = $db2->select(0, 'tb_curriculum', '*',$sqlwhere);
				foreach($curriculum as $v){
					$tmp['curriculumid'] = $v['tbid'];
					$tmp['title'] = $v['title'];
					$tmp['img'] = SITEROOTURL.$v['img'];
					$tmp['price'] = $v['price'];
					$tmp['type'] = $v['type'];
					$tmp['sales'] = $v['sales'];
					$tmp['state'] = $v['state'];
					$echo['curriculum'][] = $tmp;
					unset($tmp);
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程详情
	$app->get(
		'/curriculum/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['curriculumid'])){
				$curriculum = $db2->select(1, 'tb_curriculum', '*', array('tbid' => $data['curriculumid']));
				$echo['curriculumskuid'] = $curriculum['tbid'];
				$echo['title'] = $curriculum['title'];
				$echo['img'] = SITEROOTURL.$curriculum['img'];
				$echo['intro'] = $curriculum['intro'];
				$echo['class_time'] = $curriculum['class_time'];
				$echo['introimg'] = $curriculum['introimg'];
				$echo['price'] = $curriculum['price'];
				$echo['type'] = $curriculum['type'];
				$echo['sales'] = $curriculum['sales'];
				$echo['state'] = $curriculum['state'];
				$echo['crowd'] = $curriculum['crowd'];
				$echo['advice'] = $curriculum['advice'];
				$echo['gym_id'] = $curriculum['gym_id'];
				/* $sku = $db2->select(0, 'tb_curriculum_sku', '*', array('curriculum_id' => $curriculum['tbid']));
				foreach($sku as $v){
					$tmp['curriculumskuid'] = $v['tbid'];
					$tmp['number'] = $v['number'];
					$tmp['discount'] = $v['discount'];
					$tmp['price'] = $curriculum['price'] * $v['number'] * $v['discount'] / 10;
					$echo['sku'][]=$tmp;
				} */
				
				$tmp = array();
				$section = $db2->select(0, 'tb_curriculum_section_discount', '*', array('curriculum_id' => $curriculum['tbid']));
				foreach($section as $v){
				    $tmp['curriculumskuid'] = $v['curriculum_id'];
				    $tmp['start_sec'] = $v['start_sec'];
				    $tmp['end_sec'] = $v['end_sec'];
				    $tmp['discount'] = $v['discount'];
				    $echo['section'][]=$tmp;
				}
				
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程添加
	$app->post(
		'/curriculum/add',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] ='';
			if($data && isset($data['coachid']) && isset($data['type']) && isset($data['title']) && isset($_FILES['img']) && isset($data['price']) && isset($data['gym_id'])&& isset($data['class_time'])){
				$imgpath = 'dofiles/coach/'.$data['coachid'].'/curriculum/';
				create($imgpath);
				$imgurl = $imgpath.md5(uniqid(rand())).'.jpg';

				move_uploaded_file($_FILES['img']['tmp_name'], $imgurl);
				$set = array(
					'type' => $data['type'],
					'title' => $data['title'],
					'img' => $imgurl,
					'price' => $data['price'],
					'coach_id' => $data['coachid'],
				    'crowd' => $data['crowd'],
					'advice' => $data['advice']
				);
				if(isset($data['intro'])){
					$set['intro'] = $data['intro'];
				}
				
				if(isset($data['class_time'])){
				    $set['class_time'] = $data['class_time'];
				}
				
				if(isset($data['gym_id'])){
				    $set['gym_id'] = $data['gym_id'];
				}
				if(!empty($_FILES['introimg'])){
					$introimgurl = creatfilesimg($_FILES['introimg'],'',$data['coachid'],2,'curriculum');
					$set['introimg'] = $introimgurl;
				}
				$curriculumid = $db2->insert('tb_curriculum', $set);
				
				
				$db2->insert('tb_curriculum_sku', array(
					'curriculum_id' => $curriculumid,
					'number' => 1,
					'discount' => 10
				));
				if($data['discount']){
				    foreach($data['discount'] as $value){
				        $tmp = explode(',', $value);
				        $db2->insert('tb_curriculum_section_discount', array(
				            'curriculum_id' => $curriculumid,
				            'start_sec' => $tmp[0],
				            'end_sec' => $tmp[1],
				            'discount' => $tmp[2]
				        ));
				    }
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程编辑
	$app->post(
		'/curriculum/edit',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['type']) && isset($data['curriculumid']) && isset($data['title']) && isset($data['price']) && isset($data['discount'])){
				$imgpath = 'dofiles/coach/'.$data['coachid'].'/curriculum/';
				create($imgpath);
				$imgurl = $imgpath.md5(uniqid(rand())).'.jpg';
				move_uploaded_file($_FILES['img']['tmp_name'], $imgurl);
				$set = array(
					'type' => $data['type'],
					'title' => $data['title'],
					'img' => $imgurl,
					'price' => $data['price'],
				    'crowd' => $data['crowd'],
				    'advice' => $data['advice']
				);
				if(isset($data['intro'])){
					$set['intro'] = $data['intro'];
				}
				if(isset($data['gym_id'])){
				    $set['gym_id'] = $data['gym_id'];
				}
				if(!empty($_FILES['introimg'])){
					$introimgurl = creatfilesimg($_FILES['introimg'],'',$data['coachid'],2,'curriculum');
					$set['introimg'] = $introimgurl;
				}
				$db2->update('tb_curriculum', $set, array(
					'tbid' => $data['curriculumid'],
					'coach_id' => $data['coachid']
				));
				//更新sku
				$db2->delete('tb_curriculum_sku', array('curriculum_id'=>$data['curriculumid']));
				$db2->delete('tb_curriculum_section_discount', array('curriculum_id'=>$data['curriculumid']));
				
				$db2->insert('tb_curriculum_sku', array(
				    'curriculum_id' => $data['curriculumid'],
				    'number' => 1,
				    'discount' => 10
				));
				if($data['discount']){
				    foreach($data['discount'] as $value){
				        $tmp = explode(',', $value);
				        $db2->insert('tb_curriculum_section_discount', array(
				            'curriculum_id' => $data['curriculumid'],
				            'start_sec' => $tmp[0],
				            'end_sec' => $tmp[1],
				            'discount' => $tmp[2]
				        ));
				    }
				}
				
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程上架
	$app->post(
		'/curriculum/takeup',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['curriculumid'])){
				$curriculum=$db2->select(1,'tb_curriculum','*', array('tbid' => $data['curriculumid'],'coach_id' => $data['coachid']));
				if(empty($curriculum['tbid'])){
					$echo['error'] = "不存在该课程";
				}else if($curriculum['tbid']==1){
					$echo['error'] = "该课程已上架";
				}else{
					$db2->update('tb_curriculum',array('state'=>1), array(
						'tbid' => $data['curriculumid'],
						'coach_id' => $data['coachid']
					));
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程下架
	$app->post(
		'/curriculum/takedown',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['coachid']) && isset($data['curriculumid'])){
				$curriculum=$db2->select(1,'tb_curriculum','*', array('tbid' => $data['curriculumid'],'coach_id' => $data['coachid']));
				if(empty($curriculum['tbid'])){
					$echo['error'] = "不存在该课程";
				}else if($curriculum['tbid']==2){
					$echo['error'] = "该课程已上架";
				}else{
					$db2->update('tb_curriculum',array('state'=>2), array(
						'tbid' => $data['curriculumid'],
						'coach_id' => $data['coachid']
					));
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//课程类型
	$app->get(
		'/curriculum/type/list',
		function() use($app, $db2, $file_split, $file_subsplit,$curriculumtype){
			//echo json_encode($curriculumtype);
			
		    $sql = "select * from tb_curriculum_category order by tbid asc";
		    $ht = $db2->prepare($sql);
		    $ht->execute();
		    $echo['data'] = $ht->fetchAll(PDO::FETCH_ASSOC);
		    
		    echo json_encode($echo);
		    
		}
	);

	//我的课程
	$app->get(
		'/curriculum/mine',
		function() use($app, $db2, $file_split, $file_subsplit,$curriculumtype){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['from']) && isset($data['to']) && isset($data['userid']) && isset($data['usertype'])){
				$column='tb_curriculum_snap.tbid,tb_curriculum_snap.title,tb_curriculum_snap.type,tb_curriculum_snap.intro,tb_curriculum_snap.number,tb_order.state,('.$db2->select(4,'tb_reserve','count(*)',array('#tb_reserve.curriculum_id'=>'tb_curriculum_snap.tbid','state[!]'=>2)).')[count]';
				if($data['usertype']==2){
					$where=array('order'=>'tb_order.dt desc','tb_curriculum_snap.coach_id'=>$data['userid']);
					if(isset($data['state'])){
						$where['tb_order.state']=$data['state']+1;
					}else{
						$where['tb_order.state[!]']=array(0,1);
					}
					$column.=',('.$db2->select(4,'tb_member','nickname',array('#tb_member.tbid'=>'tb_order.userid')).')[membername]';
				}else{
					$where=array('order'=>'tb_order.dt desc','tb_order.userid'=>$data['userid'],'tb_order.usertype'=>$data['usertype']);
					if(isset($data['state'])){
						$where['tb_order.state']=$data['state']+1;
					}else{
						$where['tb_order.state[!]']=array(0,1);
					}
					$column.=',('.$db2->select(4,'tb_coach','nickname',array('#tb_coach.tbid'=>'tb_curriculum_snap.coach_id')).')[coachname]';

				}
				$echo['count'] = $db2->select(2,'tb_curriculum_snap',array('[><]tb_order'=>array('idcode')),'*',$where);
				$where['limit'] = array($data['from'], $data['to']);
				$curriculum =$db2->select(0,'tb_curriculum_snap',array('[><]tb_order'=>array('idcode')),$column,$where);
				foreach($curriculum as $value){
					$tmp['tbid']= $value['tbid'];
					$tmp['title'] = $value['title'];
					$tmp['intro'] = $value['intro'];
					$tmp['number'] = $value['number'];
					$tmp['count'] = $value['count'];
					if($data['usertype']==2){
						$tmp['coachname'] =$value['membername'];
					}else{
						$tmp['coachname'] = $value['coachname'];
					}
					$tmp['curriculumtype']=$curriculumtype[$value['type']];
					$tmp['state'] = $value['state'];
					$reserve=$db2->select(3,'tb_reserve','state',array('curriculum_id'=>$value['tbid'],'order'=>'tbid desc','limit'=>1));
					$tmp['reserve'] = $reserve;
					$echo['data'][]=$tmp;
				}
				
				//红色提醒栏-今日课程
				$tmp = date("Y-m-d");
				$begin_dt = $tmp." 00:00";
				$end_dt = $tmp." 23:59";
				$sql = "select a.tbid,b.title,b.intro,(select nickname from tb_coach  where tb_coach.tbid = b.coach_id   ) as coachname".
				",(select nickname from tb_member  where tb_member.tbid = a.member_id   ) as membername".
				",a.state,b.type,a.dt from tb_reserve as a  inner join tb_curriculum_snap as b on a.curriculum_id=b.tbid ".
				"where a.member_id = ? and a.state=1 and a.type=0 and a.dt >= ? and a.dt <= ?  order by a.dt asc";
				$ht = $db2->prepare($sql);
				$ht->execute(array($data['userid'], $begin_dt, $end_dt));
				$tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
				
				if($tmp){
    				foreach($tmp as $key => &$value){
    				    $value['reservedt'] = date('Y-m-d',strtotime($value['dt']));
    					$value['reservetime'] = date('H',strtotime($value['dt']));
    				}
				    $echo['today'] = $tmp;
				}else{
				    $echo['today'] = array();
				}
				
			}else{
				$echo['error'] = "缺少参数，请检查";
			}
			echo json_encode($echo);
		}
	);
?>
